

//
//  SampuxLogOutViewController.m
//  Sampux
//
//  Created by rzm on 16/8/11.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxLogOutViewController.h"
#import "SampuxLogOutView.h"
#import "SampuxListModel.h"
#import "SPLogOutModel.h"
#import "SampuxGetLogOutValue.h"

@interface SampuxLogOutViewController ()

@property (nonatomic, strong) SampuxLogOutView *logOutView;
//@property (nonatomic, strong) SampuxListModel *model;
@property (nonatomic, strong) NSMutableArray *tempArr;
@property (nonatomic, assign) BOOL needRefresh;

@end

@implementation SampuxLogOutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];
    self.view.backgroundColor = THSystemClearColor;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestLogOut:) name:NOTIFICATION_RESPONSE_DEVICE_RUN_RECORD_13001 object:nil];
//    [GBInterMaster_CommForDeivce requestDeviceRuns:0 withTag:1 withDeviceAccount:[[NSUserDefaults standardUserDefaults]objectForKey:@"account" ]];
    _logOutView = [[SampuxLogOutView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _logOutView.model = self.model;
    [self.view addSubview:_logOutView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.titleStr = @"运行记录";
    nav.rightBtn.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.rightBtn.hidden = YES;
}

- (void)requestLogOut:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"requestLogOut == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);

    _tempArr = [NSMutableArray array];
    if(responseCode == 0x00){
        NSMutableArray* runRecordsArr = (NSMutableArray*)[result objectForKey:@"runRecords"];
        if(runRecordsArr.count > 0){
            [_tempArr addObjectsFromArray:runRecordsArr];
        }else
        {
            [_tempArr removeAllObjects];
        }
        [self setDataModel];
    }else
    {
        [SVProgressHUD showErrorWithStatus:@"获取运行记录失败"];

//        [MBProgressHUD showError:@"获取运行记录失败"];
    }
}

- (void)setDataModel
{
    NSMutableArray *dataArr = [NSMutableArray array];
    dataArr = [SampuxGetLogOutValue getLogOutData:_tempArr];
    [self.logOutView.modelArr addObjectsFromArray:dataArr];
    self.logOutView.modelArr = self.logOutView.modelArr;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
